package com.currentcms.client.dialog;

import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class Dialog extends DialogBox
{
	private Panel _close;
	private Button _close_button;
	private SimplePanel _content;
	private FlowPanel _buttons;
	
	public Dialog()
	{
		setGlassEnabled(true);
		//setAutoHideEnabled(true);
		setModal(true);
		setSize("450px", "400px");
		super.setWidget(create_ui());
	}
	
	private Widget create_ui()
	{
		FlowPanel p = new FlowPanel();
		
		_close = new FlowPanel();
		_close.setHeight("40px");
		_close.setStyleName("xdialog-close-panel");
		_close_button = new Button();
		_close_button.addStyleName("close");
		_close_button.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				on_request_close();
			}
		});
		_close.add(_close_button);
		
		_content = new SimplePanel();
		_content.setStyleName("xdialog-content-panel");
		_buttons = new FlowPanel();
		_buttons.setHeight("40px");
		_buttons.setStyleName("xdialog-buttons-panel");

		p.add(_close);
		p.add(_content);
		p.add(_buttons);

		return p;
	}
	

	public Widget getCloseButton()
	{
		return _close_button;
	}
	
	public void setWidget(Widget w)
	{
		_content.setWidget(w);
	}
	
	public void setRelativePosition(Widget sender)
	{
		int left = sender.getAbsoluteLeft();
        int top = sender.getAbsoluteTop() ;
        if (top>Window.getClientHeight()+Window.getScrollTop()-410)
        	top = Window.getClientHeight()+Window.getScrollTop()-410;
        top -= 180;
        setPopupPosition(left, top);
	}
	
	public void setButtons(String...strings)
	{
		_buttons.clear();
		for (int i=0; i<strings.length; i++)
		{
			final String s = strings[i];
			final Button b = new Button(s);
			b.setStylePrimaryName("big-Button");
            b.getElement().getStyle().setFloat(Float.RIGHT);
			final int c = i;
			b.addClickHandler(new ClickHandler()
			{
				@Override
				public void onClick(ClickEvent event)
				{
					on_click_button(c);
				}
			});
			_buttons.add(b);
		}
	}
	
	protected void set_button_enabled(int i, boolean b)
	{
		Button btn = (Button) _buttons.getWidget(i);
		btn.setEnabled(b);
	}
	
	protected void on_click_button(int index)
	{
		System.out.println("Dialog.onClickButton "+index);
	}
	
	protected void on_request_close()
	{
		hide();
	}
}
